<style type="text/css">
		@import "http://www.rugbyclubleuven.be/components/com_php/files/dojo/resources/dojo.css";
		@import "http://www.rugbyclubleuven.be/components/com_php/files/dojox/image/resources/Magnifier.css";
div.general { margin:0px;padding:0px;float:left; }
div.newline { clear:left; }
div.pos { height:25px;width:25px;text-align:center;line-height:25px; }
div.pos2 { height:25px;width:50px;text-align:center;line-height:25px; }
div.nobuttons { height:25px;width:75px;text-align:center;line-height:25px; }

div.present { background-color:blue;}
div.absent { background-color:red;}
div.ispresent { background-color:orange; }


div.player { height:35px;width:75px;text-align:center;line-height:50px; }
div.space {width:75px;height:25px}
div.space2 {width:30px;height:25px}
div.separate {height:10px}
</style>

<script type="text/javascript" src="http://www.rugbyclubleuven.be/components/com_php/files/dojo/dojo.js" djConfig="isDebug: true, parseOnLoad:true"></script>
<!-- for debugging -->
<script type="text/javascript" src="http://www.rugbyclubleuven.be/components/com_php/files/dojox/image/MagnifierLite.js"></script>
<script type="text/javascript" src="http://www.rugbyclubleuven.be/components/com_php/files/dojox/image/Magnifier.js"></script>

<script type="text/javascript">
        dojo.require("dojox.image.Magnifier");
	dojo.require("dijit.form.Button");		
</script>
<?php

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// retrieve user instance
$user =& JFactory::getUser();

// check if user is logged in
if ($user->id) {
    $lidid=0;
    $conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
    $stmt = "select lidid from faml where login='" . $my->username . "'";
    $query = ibase_query($conn, $stmt);
    while ($row = ibase_fetch_object($query)){
        $lidid=$row->LIDID;
    }
    ibase_free_result($query);
    ibase_close($conn);
} 

if ($_GET['confirm']){
    $conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
    $stmt = "update wede set SWBEV='T' where lidid=" . $_GET['playerid'] . " and wedid = " . $_GET['confirm'];
    $query = ibase_query($conn, $stmt);
    ibase_close($conn);
} 
if ($_GET['deny']){
    $conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
    $stmt = "update wede set SWBEV='F' where lidid=" . $_GET['playerid'] . " and wedid = " . $_GET['deny'];
    $query = ibase_query($conn, $stmt);
    ibase_close($conn);
}


//$date='24.10.2010';

$players=array('???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???','???');
$playerids=array();
$aanweziglijst=array('U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U','U');

$conn=ibase_connect('database.rugbyclubleuven.be:/opt/interbase/data/rcl.fdb', 'werker', 'rclrcl'); 
$wedstmt = "select wed.wedid from weds wed where wed.datum >=" . current_date . " and wed.ploid=" . getPloegId() . " order by wed.datum rows 1";
$wedquery = ibase_query($conn, $wedstmt);
while ($row = ibase_fetch_object($wedquery)){
    $wedid=$row->WEDID;
}
ibase_free_result($wedquery);
$stmt = "select wed.tegen,wed.wedid,lid.lidid, lid.rnaam, aanw.posit, aanw.swbev from weds wed ";
$stmt .=" join wede aanw on wed.wedid=aanw.wedid ";
$stmt .=" join faml lid on aanw.lidid = lid.lidid ";
$stmt .=" where wed.wedid=" . $wedid . " and aanw.posit !=0";
$query = ibase_query($conn, $stmt);
$reserve=16;
while ($row = ibase_fetch_object($query)){
    $tegen=$row->TEGEN;
    $wedid=$row->WEDID;
    if ($row->POSIT =='16'){
       $posit=$reserve;
       $reserve++;
    } else {
       $posit=$row->POSIT;
    }
    $players[$posit]=$row->RNAAM;
    $playerids[$posit]=$row->LIDID;
    if ($row->SWBEV == 'T'){
        $aanweziglijst[$posit]='Y';
    } else if ($row->SWBEV == 'F'){
        $aanweziglijst[$posit]='N';
    } else {
        $aanweziglijst[$posit]='U';
    }
}
ibase_free_result($query);
ibase_close($conn);

echo 'Deze zondag spelen we tegen ' . $tegen . '...De opstelling is de volgende<br/><br/>';

$picfolder="/images/stories/spelers/" . getMap() . "/big/";
$spelerpics=array();
for ($i=1;$i<count($players);$i++){
   $spelerpics[$i]=$picfolder . 'lid' . $playerids[$i] . "_profile.jpg";
}

function displayRow($positions,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid){
   $result="";
   for ($idx = 1;$idx <=7; $idx +=1){
      if ($positions[$idx]!='?' && ($positions[$idx]<=15 || $players[$positions[$idx]]!='???')){
         $result .="<div class='general" . getNewline($idx) . "'>";
         $result .= displaySpelerPicture($spelerpics[$positions[$idx]]);
         $result .= "</div>";
         $result .= "<div class='general" . isAanwezig($aanweziglijst,$positions[$idx]) . " pos2'><font color='#FFFFFF'>";
         $result .= $positions[$idx];
         $result .= "</font></div>";
      } else {
         $result .="<div class='general space" . getNewline($idx) . "'></div>";       
      }
   }
   for ($idx = 1;$idx <=7; $idx +=1){
      if ($positions[$idx]!='?' && ($positions[$idx]<=15 || $players[$positions[$idx]]!='???')){
         $result .="<div class='general" . getNewline($idx) . isAanwezig($aanweziglijst,$positions[$idx]) . " player'>";
         $result .= displayName(strval($positions[$idx]),$playerids,$players);
         $result .= "</div>";
      } else {
         $result .="<div class='general space" . getNewline($idx) . "'></div>";       
      }
   }
   for ($idx = 1;$idx <=7; $idx +=1){
      if ($positions[$idx]!='?' && ($positions[$idx]<=15 || $players[$positions[$idx]]!='???')){
        if (($lidid==$playerids[$positions[$idx]]) || ($lidid==356) || ($lidid==181) || ($lidid==121)){
           if ($aanweziglijst[$positions[$idx]]=='U'){
              $result .=addButtonsRow($idx,$playerids,$positions[$idx],$wedid,$aanweziglijst);
           } else {
              $result .="<div class='general" . getNewline($idx) . isAanwezig($aanweziglijst,$positions[$idx]) . " nobuttons'></div>";
           }
        } else {
           $result .="<div class='general" . getNewline($idx) . isAanwezig($aanweziglijst,$positions[$idx]) . " nobuttons'></div>";       
        }
      } else {
         $result .="<div class='general space" . getNewline($idx) . "'></div>";       
      }
   }
   return $result;
}

function addButtonsRow($idx,$playerids,$position,$wedid,$aanweziglijst){
    $result = "<div class='general" . getNewline($idx) .  isAanwezig($aanweziglijst,$position) . " pos'>";
    $result .="<a href=http://www.rugbyclubleuven.be/index.php/" . getPath() . "/" . getOpstPath() ."?confirm=" . $wedid . "&playerid=" . $playerids[$position] . "><img width=25 height=25 src=http://www.rugbyclubleuven.be/images/smilies/happy.png></a></div>";
    $result .="<div class='general" .  isAanwezig($aanweziglijst,$position) . " pos'></div>";
    $result .="<div class='general" .  isAanwezig($aanweziglijst,$position) . " pos'>";
    $result .="<a href=http://www.rugbyclubleuven.be/index.php/" . getPath() . "/" . getOpstPath() . "?deny=" . $wedid . "&playerid=" . $playerids[$position] . "><img width=25 height=25 src=http://www.rugbyclubleuven.be/images/smilies/weepy.png></a></div>";
    return $result;
}

function getNewLine($idx){
   if ($idx == 1){
       return " newline";
   }
}

function displayName($pos,$playerids,$players){
    return '<a href=http://www.rugbyclubleuven.be/index.php/' . getPath() . '/spelers?lid=' . $playerids[$pos] . '><font color="#FFFFFF">' . $players[$pos] . '</font></a>';
}

function displaySpelerPicture($pic){
   $filename="/home/rugbyclubleuven/www1/html" . $pic;
   if (file_exists($filename)){ 
       return "<img dojoType='dojox.image.MagnifierLite' src='" . $pic . "' style='width:25px; height:25px;' scale='5.8' />";
   } else {
       return "<img dojoType='dojox.image.MagnifierLite' src='http://www.rugbyclubleuven.be/images/stories/spelers/dummy-person.jpg' style='width:25px; height:25px;' scale='5.8' />";
   }
}  

function isAanwezig($aanweziglijst,$position){

   if ($aanweziglijst[$position]=='N'){
      return " absent";
   } else if ($aanweziglijst[$position]=='U'){
      return " ispresent";
   } else if ($aanweziglijst[$position]=='Y'){
      return " present";
   }
}

?>
<form action="confirm" method="get">
<?php
$positionRow=array('nothing',1,'?',2,'?',3,'?','?');
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
$positionRow=array('nothing','?',4,'?',5,'?','?','?');
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
$positionRow=array('nothing',6,'?',8,'?',7,'?','?');
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
echo "<div class='general separate newline'></div>";
$positionRow=array('nothing','?','?',9,'?','?','?','?');
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
$positionRow=array('nothing','?','?','?',10,'?','?','?');
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
$positionRow=array('nothing','?','?','?','?',12,'?','?');
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
$positionRow=array('nothing','?','?','?','?','?',13,'?');
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
$positionRow=array('nothing',11,'?',15,'?','?','?',14);
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
echo "<div class='general separate newline'></div>";
echo "<div class='general separate newline'>**********************************************  Reserven  ***************************************************</div>";
echo "<div class='general separate newline'></div>";
$positionRow=array('nothing',16,'?',17,'?',18,'?',19);
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
echo "<div class='general separate newline'></div>";
$positionRow=array('nothing',20,'?',21,'?',22,'?',23);
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
$positionRow=array('nothing',24,'?',25,'?',26,'?',27);
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
$positionRow=array('nothing',28,'?',29,'?',30,'?','?');
echo displayRow($positionRow,$spelerpics,$aanweziglijst,$playerids,$players,$wedid,$lidid);
?>
</form>